package com.yilin.platform.controller.user;

import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.application.UserUtil;
import com.yilin.platform.base_core.commons.annotation.SafeVerify;
import com.yilin.platform.base_core.commons.annotation.SkipLoginVerify;
import com.yilin.platform.base_core.commons.annotation.UserTarget;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.entity.enums.user.RoleType;
import com.yilin.platform.entity.enums.user.UserType;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.entity.user.Organize;
import com.yilin.platform.service.user.IAccountService;
import com.yilin.platform.service.user.IOrganizeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 组织管理
 */
@RestController
@RequestMapping("/user/organize/")
public class OrganizeController extends BaseController {

    @Resource
    private IAccountService accountService;
    @Resource
    private IOrganizeService organizeService;

    /**
     * 根据组织ID获取所有下级组织
     */
    @PostMapping("getBelowOrganizePage")
    public ReturnData<PageData<Organize>> getBelowOrganizePage(Organize query, Integer page, Integer limit) {
        if (StringUtil.isEmpty(query.getId())) query.setId(getLoginUser().getOrganizeId());
        if (StringUtil.isEmpty(query.getSort())) query.setSort("userType-ASC");
        PageData<Organize> childOrganizePage = organizeService.getBelowOrganizePage(query.getId(),query, page, limit);
        return jsonView(true, "获取成功", childOrganizePage);
    }

    /**
     * 获取所有授权组织
     */
    @PostMapping("getAuthorizedOrganizePage")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<PageData<Organize>> getAuthorizedOrganizePage(Organize query, Integer page, Integer limit) {
        ;
        PageData<Organize> childOrganizePage = organizeService.getAuthorizedOrganizePage(query, page, limit);
        return jsonView(true, "获取成功", childOrganizePage);
    }

    /**
     * 修改授权组织
     */
    @PostMapping("updateAuthorizedOrganize")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<PageData<Organize>> updateAuthorizedOrganize(String organizeId, Double number, Account.UserState userState) {
        ParamUtil.isNull(organizeId, "组织ID为空");
        ParamUtil.isNull(number, "组织ID为空");
        ParamUtil.isNull(userState, "组织ID为空");
        Organize organize = organizeService.getOrganizeById(organizeId);
        ParamUtil.isNull(organize, "获取组织信息失败");
        organize.setTruckNumber(number);
        organize.setAuthorizedState(userState);
        organizeService.updateById(organize);
        return jsonView(true, "保存成功");
    }

    /**

    /**
     * 根据组织ID获取直属下级组织
     */
    @PostMapping("getChildOrganizePage")
    public ReturnData<PageData<Organize>> getChildOrganizePage(Organize query, Integer page, Integer limit) {
        if (StringUtil.isEmpty(query.getId())) query.setId(getLoginUser().getOrganizeId());
        PageData<Organize> childOrganizePage = organizeService.getLowerOrganizePage(query, page, limit);
        return jsonView(true, "获取成功", childOrganizePage);
    }

    /**
     * 根据组织ID获取组织信息
     */
    @PostMapping("getOrganizeById")
    public ReturnData<Organize> getOrganizeById(String organizeId) {
        if (StringUtil.isEmpty(organizeId)) return jsonView(false, "组织ID不能为空！");
        Organize organize = organizeService.getOrganizeById(organizeId);
        return jsonView(true, "获取成功", organize);
    }

    /**
     * 批量ID查询
     */
    @PostMapping("getOrganizeListByIds")
    public ReturnData<List<Organize>> getOrganizeListByIds(String[] organizeIds) {
        List<Organize> organizeByQuery = organizeService.getOrganizeListByIds(organizeIds);
        return jsonView(true, "获取成功", organizeByQuery);
    }

    /**
     * 根据组织ID获取员工
     */
    @PostMapping("getEmployeeByOrganizeId")
    public ReturnData<List<Account>> getEmployeeByOrganizeId(String organizeId) {
        if (organizeId == null) organizeId = getLoginUser().getOrganizeId();
        List<Account> employeeByOrganizeId = accountService.getEmployeeByOrganizeId(organizeId);
        return jsonView(true, "获取成功", employeeByOrganizeId);
    }


    /**
     * 获取组织详细资料
     */
    @PostMapping("getUserOrganizeDetails")
    public ReturnData<Organize> getUserOrganizeDetails(String userId, String organizeId) {
        Organize organize;
        if (StringUtil.isNotEmpty(organizeId)) {
            organize = organizeService.getOrganizeById(organizeId);
        } else if (StringUtil.isNotEmpty(userId)) {
            Account user = accountService.getAccountById(userId);
            organize = organizeService.getOrganizeById(user.getOrganizeId());
        } else {
            organize = organizeService.getOrganizeById(getLoginUser().getOrganizeId());
        }
        //组织员工列表
        organize.setEmployeeList(accountService.getEmployeeByOrganizeId(organize.getId()));
        return jsonView(SUCCESS_CODE, true, "获取成功", organize);
    }

    /**
     * 修改组织和管理账户信息
     *
     * @param account  account里面的organize 优先级比外层organize优先级高
     * @param organize 同时传入时优先account里的organize
     */
    @PostMapping("updateUserInfo")
    public ReturnData<Object> updateUserInfo(Account account, Organize organize) {
        if (organize != null && organize.getId() == null) organize.setId(getLoginUser().getOrganizeId());
        if (account.getId() == null) account.setId(getLoginUser().getId());
        if (account.getOrganize() != null) organize = account.getOrganize();
        Account localAccount = accountService.getAccountById(account.getId());
        if (localAccount == null) return jsonView(false, "未查询到该用户");
        Organize localOrganize = organizeService.getOrganizeById(localAccount.getOrganizeId());
        if (localOrganize == null) return jsonView(false, "未查询到该组织");
        if (getLoginUser().getRoleType() == RoleType.employee) {
            return jsonView(false, "您没有权限修改信息！");
        }
        if (getLoginUser().getUserType().ordinal() > localOrganize.getUserType().ordinal()) {
            return jsonView(false, "您没有权限修改信息！");
        }
        if (getLoginUser().getUserType() == localOrganize.getUserType() && !getLoginUser().getId().equals(localAccount.getId())) {
            return jsonView(false, "只能修改您自己的信息");
        }
        accountService.updateAccount(getLoginUser(), account, localAccount);
        if (organize != null) organizeService.updateOrganize(getLoginUser(), organize, localOrganize);
        UserUtil.updateLoginUser(localAccount);
        return jsonView(true, "修改成功");
    }

    /**
     * 修改组织信息
     */
    @PostMapping("updateOrganize")
    public ReturnData<Object> updateOrganize(Organize organize) {
        if (organize.getId() == null) organize.setId(getLoginUser().getOrganizeId());
        Organize localOrganize = organizeService.getOrganizeById(organize.getId());
        if (localOrganize == null) return jsonView(false, "未查询到该组织");
        if (getLoginUser().getUserType().ordinal() > localOrganize.getUserType().ordinal()) {
            return jsonView(false, "您没有权限修改组织信息！");
        }
        if (getLoginUser().getUserType() == localOrganize.getUserType() && getLoginUser().getRoleType() != RoleType.manager) {
            return jsonView(false, "您没有权限修改组织信息");
        }
        organizeService.updateOrganize(getLoginUser(), organize, localOrganize);
        return jsonView(true, "修改成功");
    }

    /**
     * 解散组织
     */
    @PostMapping("dismissOrganize")
    @SafeVerify
    public ReturnData<Object> dismissOrganize() {
        if (getLoginUser().getRoleType() != RoleType.manager) {
            return jsonView(false, "仅管理员可操作此功能！");
        }
        Organize organize = organizeService.getOrganizeById(getLoginUser().getOrganizeId());
        //实体中含有逻辑删除字段，此数据不会被真实删除
        organizeService.dismissOrganize(getLoginUser(), organize);
        return jsonView(SUCCESS_CODE, "操作成功");
    }



    //获取所有车队组织
    @PostMapping("getTruckOrganizeList")
    @ResponseBody
    @SkipLoginVerify
    public ReturnData<Object> getTruckOrganizeList() {
        List<Organize> list = organizeService.getTruckOrganizeList();
        return jsonView(true, "获取成功", list);
    }

    //获取所有车队组织
    @PostMapping("getTruckOrganizePage")
    @ResponseBody
    @SkipLoginVerify
    public ReturnData<Object> getTruckOrganizePage(Organize query, Integer page, Integer limit) {
        PageData<Organize> list = organizeService.getTruckOrganizePage(query,page,limit);
        return jsonView(true, "获取成功", list);
    }

}
